<?php 
/**
 * 菜品展示介绍
 * @author Administrator
 *
 */
class ProductAction extends BaseAction
{

    public function index()
    {
        //实例化model
        $ProductModel = new ProductModel();

        $count = $ProductModel->getAllcont();

        //实例化分页类
        //载入分页扩展类
        import('ORG.Util.Page');
        $page = new Page($count, 7);

        //分页字符
        $pageStr = $page->firstRow.','.$page->listRows;

        if ( $_GET['seek'] == 1){
            if ($_GET['ty_id']!=''){
                $seekStr['ty_id'] = $_GET['ty_id'];
            }
            if ($_GET['td_name']!=''){
                $seekStr['td_name'] = $_GET['td_name'];
            }

        }

        $results = $ProductModel->getAllnr($pageStr, $seekStr);

        //数据重装
        foreach ( $results as $k=>&$v )
        {
            if ($v['is_show']=='1'){
                $v['is_show'] = '<span style="color: #00aa00">显示</span>';
            }else{
                $v['is_show'] = '<span style="color: red">隐藏</span>';
            }

        }

        //获取所有顶级分类
        $TypeModel = new TypeModel();
        $type = $TypeModel->getListAll();
        $this->type = $type;

        $this->results = $results;

        $this->show = $page->show();

        $this->display();

    }






    public function index_tow()
	{

			if ( $_GET['seek'] == 1 )
			{
			
				//搜索字段数组
				$field = array(

                        'ty_id',

				);
		
				$seek = common_html_Arr($_GET);
			
				//过滤空数组
				$seek = common_filtration($seek);
				
				//过滤非法字段
				foreach ( $seek as $k=>$v )
				{
						
					if ( !in_array($k, $field) )
					{
						unset($seek[$k]);
						continue;
					}
					
					
					
					$k == 'ty_id' ? $seekStr .= "LOCATE('{$v}',{$k}) > 0  and " : $seekStr .= $k."='{$v}' and ";

				}

			}


        //P($seekStr);
			//载入分页扩展类
			import('ORG.Util.Page');
				
			//实例化model
			$ProductModel = new ProductModel();
			
			$count = $ProductModel->getCount($seekStr);
			
			//实例化分页类
			$page = new Page($count, 4);
				
			//分页字符
			$pageStr = $page->firstRow.','.$page->listRows;
				
			$results = $ProductModel->getListAll($pageStr, $seekStr);
			
			//die(P($results['add_time']));
			
			//数据重装
			foreach ( $results as $k=>&$v )
			{
				//时间格式转化
				$v['add_time'] = date('Y-m-d', $v['add_time']);
				//省略
				$v['td_text']=common_subtext($v['td_text'],40);

			}
			//die(P($results));
        //获取所有顶级分类
        $TypeModel = new TypeModel();
        $type = $TypeModel->getListAll();
        $this->type = $type;
	
			$this->results = $results;

			$this->show = $page->show();
			
			$this->display();
	}
	
	
	/**
	 * 更新（插入）视图
	 */
	public function update()
	{
		
		//菜品种类model
		$typeModel = new TypeModel();
		//更新
		if ( common_is_number($_GET['id']) )
		{
			$id = common_html_Arr($_GET['id']);
	
			//model
			$ProductModel = new ProductModel();
			//获取指定id数据
			$result = $ProductModel->getRowById($id);

			//获取指定ID顶级分类
			$parent = $typeModel->getRowByNode($result['ty_id']);

			//获取节点
			$node = $typeModel->getNode($parent['ty_id']);

			//获取科室的分类
            $typeModel = new TypeModel();
            $f_pid = $typeModel->getRowById($result['ty_id']);



			//重组节点
			foreach ( $node as $k=>$v )
			{
				//重置顺序
				if ( $v['ty_id'] == $result['ty_id'] )
				{
					unset($node[$k]);
			
					array_unshift($node, $v);
			
					continue;
			
				}
		
			}

			//die(P($node));
			//前端注入变量

            $this->f_pid = $f_pid;

			$this->node = $node;

			$this->result = $result;
	
			//die(P($result));
			
		}




		//获取所有顶级分类
		$TypeModel = new TypeModel();
		$type = $TypeModel->getListAll();
        $this->type = $type;
        //P($type);


		//die(P($type));
		//前端注入变量
		$this->type = $type;
						
		$this->display();
			
	}



	/**
	 *更新（插入）团队方法
	 */
	public function doUpdate()
	{

		IS_POST || die('非法操作');
		//参数过滤
		$requestArr = $_POST;
			//P($requestArr);
		//die(P($_POST));
		//校验厨师名
			if ( common_is_names($requestArr['td_name'], 2, 12) == NULL )
			{
				//响应状态
				$response['status'] = 0;
					
				//响应信息
				$response['info'] = '姓名格式错误';
					
				die(json_encode($response));
					
			}

        $requestArr['add_time'] = date("Y-m-d H:i:s");


		//实例化model
		$ProductModel = new ProductModel();
		//插入
		if ( common_is_number($requestArr['td_id']) == NULL )
		{
		//插入数据
            if ($requestArr['sort']==''){
                $requestArr['sort']='1';
            }

			$result = $ProductModel->insert($requestArr);
			
			if ( $result > 0 )
			{
				$response['status'] = 1;
				
				$response['info'] = '添加成功';
			}
			else 
			{
				$response['status'] = 0;
					
				$response['info'] = '添加失败';
			}
	
	
		}
		//更新
		else
		{

		    //更新数据

			$result = $ProductModel->update($requestArr, $requestArr['td_id']);

			if ( $result !== false )
			{
				$response['status'] = 1;
					
				$response['info'] = '修改成功';
			}
			else
			{
				$response['status'] = 0;
	
				$response['info'] = '修改失败';
			}
	
		}
			
		die(json_encode($response));
			
		
	}
	
	/**
	 * 上传头像图片处理方法
	 */
	public function uploadImg1()
	{
	
		//上传路径配置
		$path = C('IMG_UPLOAD_PATH').$_SESSION['console']['user_id'].'/'. date("Y_m_d").'/';
        //P($path);
		//上传文件
		$result = common_upload_img($path);

		//配置返回上传信息
		if ( !$result )
		{
			//错误信息
			$upInfo['status'] = 0;
			$upInfo['info'] = '上传文件失败';
	
		}
		else
		{
			//成功返回路径
			$upInfo['status'] = 1;
	
			$upInfo['path'] = substr($result[0]['savepath'].$result[0]['savename'], 1);
	
		}
	
	
		//返回上传信息
		die(json_encode($upInfo));
	
	}
	/**
	 * 上传图片处理方法
	 */
	public function uploadImg()
	{
	
		//上传路径配置
		$path = C('IMG_UPLOAD_PATH').$_SESSION['console']['user_id'].'/';
	
		//上传文件
		$result = common_upload_img($path);
	
		//配置返回上传信息
		if ( !$result )
		{
			//错误信息
			$upInfo['status'] = 0;
			$upInfo['info'] = '上传文件失败';
	
		}
		else
		{
			//成功返回路径
			$upInfo['status'] = 1;
	
			$upInfo['path'] = substr($result[0]['savepath'].$result[0]['savename'], 1);
	
		}
	
	
		//返回上传信息
		die(json_encode($upInfo));
	
	}
	/**
	 *删除控制
	 */
	public function del()
	{
			
		IS_AJAX || die('非法提交');
			
		$requset = common_html_Arr($_POST);
			
		//ProductModel model
		$ProductModel = new ProductModel();
			
		$result = $ProductModel->del($requset['check']);
			
		if ( $result > 0 )
		{
			$response['status'] = 1;
	
			$response['info'] = '删除成功';
		}
		else
		{
	
			$response['status'] = 0;
	
			$response['info'] = '删除失败';
	
		}
			
		die(json_encode($response));
	}
}

?>